package com.example.translateappjava.DataBase;

import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import androidx.room.Update;

import com.example.translateappjava.Utils.Post;

import java.util.List;

@Dao
public interface PostDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertAllPosts(List<Post> posts);

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertPost(Post post); // Room会使用 post.localId 作为主键

    @Update
    void updatePost(Post post); // Room会根据 post.localId 来更新

    @Query("SELECT * FROM posts ORDER BY time DESC")
    List<Post> getAllPosts();

    // 根据服务器ID查询帖子
    @Query("SELECT * FROM posts WHERE serverId = :serverIdFromArg")
    Post getPostByServerId(Long serverIdFromArg); // 参数名可以任意，但查询中要对应

    // (可选) 根据本地ID查询帖子
    @Query("SELECT * FROM posts WHERE localId = :localIdFromArg")
    Post getPostByLocalId(Long localIdFromArg);

    @Query("DELETE FROM posts")
    void deleteAllPosts();
}